var rootPath = getRootPath();

$(function() {
	$.ajax({
		url: rootPath + '/grade/queryGradesBySomeFields',
        type: 'post',
        contentType : 'application/json;charset=UTF-8',
        data: JSON.stringify({
        	"dr" : 1
        }),
        success: function (data) {
        	fillGrade(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error:(");
        }
    });
	
	$.ajax({
		url: rootPath + '/grade/pid/0/dr/1',
        type: 'GET',
        contentType : 'application/json;charset=UTF-8',
        success: function (data) {
        	if(data.returnCode == "0000") {
        		var option = "<option name='pid' value=''>请选择级别分类</option>";
        		var length = data.returnObject.length;
        		for(var i = 0;i < length;i++) {
        			option += "<option name='pid' value='" + data.returnObject[i].gid + "'>" + data.returnObject[i].gname + "</option>";
        		}
        		$("#pGrades").append(option);
        	} else {
        		layer.alert(data.returnMessage);
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error:(");
        }
    });
	
	$("#gname").keyup(function(event) {
		if(event.keyCode == 13) {
			$("#searchGrade").click();
		}
	});
	$("#searchGrade").click(function() {
		var dr = $("#dr").val();
		var pid = $("option:selected").val();
		var gname = $("#gname").val();
		$.ajax({
			url: rootPath + '/grade/queryGradesBySomeFields',
	        type: 'POST',
	        data : JSON.stringify({
	        	"dr" : dr,
	        	"pid" : pid,
	        	"gname" : gname
	        }),
	        contentType : 'application/json;charset=UTF-8',
	        success: function (data) {
	        	$("#grades").empty();
	        	fillGrade(data);
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	        	layer.alert("error:(");
	        }
	    });
	});
});

/*级别-添加-编辑*/
function grade_add_edit(gname,url,w,h){
	layer_show(gname,url,w,h);
}

function grade_del(obj,id){
	var dataNum = parseInt($("#dataNum").html());
	layer.confirm('删除操作须谨慎,若删除的级别含有子级别,则同时删除子级别,确认要删除吗?',function(index){
		if(id != "") {
			$.ajax({
				url: rootPath + '/grade/' + id,
				type: 'DELETE',
				success: function(data){
					if(data.returnCode == "0000") {						
						$(obj).parents("tr").remove();
						layer.msg('已删除!',{icon:1,time:1000},function() {
							$.ajax({
								url: rootPath + '/grade/queryGradesBySomeFields',
						        type: 'post',
						        contentType : 'application/json;charset=UTF-8',
						        data: JSON.stringify({
						        	"dr" : 1
						        }),
						        success: function (data) {
						        	fillGrade(data);
						        }
						    });
						});
					} else {
						layer.alert(data.returnMessage);
					}
				},
				error:function(data) {
					layer.alert("error:(");
				},
			});		
		}
	});
}

function fillGrade(data) {
	if(data.returnCode == "0000") {
		var length = data.returnObject.list.length;
		$("#dataNum").html(data.returnObject.total);
		var tr;
		for(var i = 0;i < length;i++) {
			tr += "<tr class='text-c' name='gradeTr'>";
			tr += "<td>" + data.returnObject.list[i].gname + "</td>";
			tr += "<td>" + ((data.returnObject.list[i].pGrade == undefined) ? "" : data.returnObject.list[i].pGrade.gname) + "</td><td>";
			tr += "<a gname='编辑' href='javascript:;' onclick='grade_add_edit(\"编辑级别\"," + "\"gradeEditPage?gid=" + data.returnObject.list[i].gid + "\", \"\",\"\")' style='text-decoration:none'><i class='iconfont marginLeft12'>&#xe60a;</i></a>";
			tr += "<a gname='删除' href='javascript:;' onclick='grade_del(this," + "\"" + data.returnObject.list[i].gid + "\")' class='ml-5' style='text-decoration:none'><i class='iconfont marginLeft12'>&#xe612;</i></a></td></tr>";
		}
		$("#grades").empty();
		$("#grades").append(tr);
		
		var page = "";
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ 1 + ")'>首页</span>";
		if (data.returnObject.hasPreviousPage) {
			page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.prePage + ")'>上一页</span>";
		}
		var num = data.returnObject.navigatepageNums.length;
		for (var i = 0; i < num; i++) {
			if (data.returnObject.navigatepageNums[i] == data.returnObject.pageNum) {
				page += "<span style='font-weight:900;padding:7px 10px;'>"
						+ data.returnObject.pageNum + "</span>";
			} else {
				page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
						+ data.returnObject.navigatepageNums[i]
						+ ")'>"
						+ data.returnObject.navigatepageNums[i] + "</span>";
			}
		}
		if (data.returnObject.hasNextPage) {
			page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.nextPage + ")'>下一页</span>";
		}
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ data.returnObject.pages + ")'>末页</span>";

		$("#page").empty();
		$("#page").append(page);
		
		$(".pageNavigate").mouseover(function() {
			$(this).css("cursor", "pointer");
		});
	} else {
		$("#dataNum").html("0");
	}
}

function switchPage(pageNo) {
	$(this).css("cursor", "pointer");
	var dr = $("#dr").val();
	var pid = $("option:selected").val();
	var gname = $("#gname").val();
	$.ajax({
		url: rootPath + '/grade/queryGradesBySomeFields',
        type: 'post',
        contentType : 'application/json;charset=UTF-8',
		data : JSON.stringify({
			"dr" : dr,
			"pid" : pid,
			"gname" : gname,
			"pageNo" : pageNo
		}),
		success : function(data) {
			fillGrade(data);
			$(".pageNavigate").hover(function() {
				$(this).css("cursor", "pointer");
			});
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});
}